package com.scst.service.impl;

import com.scst.constant.MessageConstant;
import com.scst.dto.TeacherLoginDTO;
import com.scst.entity.Teacher;
import com.scst.exception.PasswordErrorException;
import com.scst.mapper.TeacherMapper;
import com.scst.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.AccountNotFoundException;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Teacher login(TeacherLoginDTO teacherLoginDTO) throws AccountNotFoundException {
        String username=teacherLoginDTO.getUsername();
        String password=teacherLoginDTO.getPassword();
        //1、根据用户名查询数据库中的数据
        Teacher teacher = teacherMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (teacher== null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (!password.equals(teacher.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //3、返回实体对象
        return teacher;
    }
}
